﻿package com.quickdev.intelligenthome.utils
{
	import flash.utils.ByteArray;

	public class CRC16 extends CRC
	{
		private const crcGen :uint = 0xA001;
		
		public function CRC16()
		{
		}
		
		override public function reset():void {
			crc = 0xFFFF;
		}
		
		override public function update(buffer:ByteArray, offset:uint=0, length:int=-1):void {
			if( length == -1 ) {
				length = buffer.length;
			}
			var temp :uint;
			for (var i:int=offset; i < length; i++) {  
				temp = uint( buffer[i] );
				temp <<= 8;
				crc ^= temp;
				for(var j:int = 0; j < 8; ++j) {
					if( crc & 0x01 ) {
						crc >>= 1;
						crc ^= crcGen;
					}
					else {
						crc >>= 1;
					}
				}
			}
		}
	}
}